//
//  UIView+YMAddition.swift
//  YMToolKit
//
//  Created by YDWY on 2017/4/20.
//  Copyright © 2017年 YDWY. All rights reserved.
//

import Foundation
import UIKit



extension UIView {

    
    ///返回视图截图
    public func ym_snapshotImage() -> UIImage {
    
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 0)
        
        self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
        
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        
        return result ?? UIImage()
    }
    
    
    // 根据View 查找vc
    public func ym_responderViewController() -> UIViewController {
        var responder: UIResponder! = nil
        var next = self.superview
        while next != nil {
            responder = next?.next
            if (responder!.isKind(of: UIViewController.self)){
                return (responder as! UIViewController)
            }
            next = next?.superview
        }
        return (responder as! UIViewController)
    }
    
    
    
    /// 返回视图的 x轴 的起点坐标
    public var ym_x : CGFloat {
        
        get{
            return self.frame.origin.x
        }
        set{
            var r = self.frame
            r.origin.x = newValue
            self.frame = r
        }
        
    }
    
    
    /// 返回视图 y轴 的起点坐标
    public var ym_y : CGFloat {
        
        get{
            return self.frame.origin.y
        }
        
        set{
            var r = self.frame
            r.origin.y = newValue
            self.frame = r
        }
        
    }
    
    
    
    ///返回视图的 宽
    public var ym_width : CGFloat {
        
        get{
            return self.frame.size.width
        }
        
        set{
            var r = self.frame
            r.size.width = newValue
            self.frame = r
        }
     
    }
    

    
    /// 返回视图的 高
    public var ym_heigth : CGFloat {
    
        get {
            return self.frame.size.height
        }
        
        set{
            var r = self.frame
            r.size.height = newValue
            self.frame = r
        }
    }
    
    
    
    
    /// 视图右边界的 X 值
    public var ym_rightX : CGFloat {
    
        get{
            return self.ym_x + self.ym_width
        }
        set{
            var r = self.frame
            r.origin.x = newValue - frame.size.width
            self.frame = r
        }
    }
    
    
    /// 下边界的 Y 值
    public var ym_bottomY : CGFloat{
    
        get{
            return self.ym_y + self.ym_heigth
        }
        
        set{
            var  r = self.frame
            r.origin.y = newValue - frame.size.height
            self.frame = r
        }
        
    }
    
    
    /// 中心的 x 点
    public var ym_centerX : CGFloat{
        get{
            return self.center.x
        }
        set{
            self.center = CGPoint(x: newValue, y: self.center.y)
        }
    }
    
    
    
    /// 视图中心的 y 点
    public var ym_centerY : CGFloat{
        get{
            return self.center.y
        }
        set{
            self.center = CGPoint(x: self.center.x, y: newValue)
        }
    }
    
    
    
    /// 视图的起点  坐标
    public var ym_origin : CGPoint {
        
        get{
            return self.frame.origin
            
        }
        
        set{
            self.ym_x = newValue.x
            self.ym_y = newValue.y
        }
    }
    
    
    /// 视图的 大小
    public var ym_size: CGSize{
        get{
            return self.frame.size
        }
        set{
            self.ym_width = newValue.width
            self.ym_heigth = newValue.height
        }
    }
    

}
